package com.jiejie.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiejie.commons.ServerResponse;
import com.jiejie.entity.Customer;
import com.jiejie.entity.LoginCustomer;
import com.jiejie.entity.Schedule;
import com.jiejie.mapper.CustomerMapper;
import com.jiejie.mapper.ScheduleMapper;
import com.jiejie.service.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiejie.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 辰广
 * @since 2022-10-05
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer>  implements ICustomerService {

    @Resource
    private CustomerMapper customerMapper;
    //密码登录
    @Override
    public ServerResponse login(Customer customer) {
        //1. DB 验证用户名与密码
        QueryWrapper<Customer> wrapper=new QueryWrapper<>();
        wrapper.eq("customer_name",customer.getCustomerName());
        wrapper.eq("customer_password",customer.getCustomerPassword());

        Customer selectOne=customerMapper.selectOne(wrapper);
        System.out.println("查询到的登录账户：" + selectOne);
        LoginCustomer loginCustomer=new LoginCustomer(selectOne.getCustomerId(),selectOne.getCustomerName());



            if (loginCustomer!=null){
                //2. JWT创建token
                String token= TokenUtil.sign(loginCustomer);
                System.out.println("service token:" + token);
                return ServerResponse.success("登录成功",token);
                }

        return ServerResponse.fail("登录失败",null);
    }

    @Override
    public ServerResponse regist(Customer customer) {

        //验证用户名是否存在
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("customer_name", customer.getCustomerName());

        Customer selectOne = customerMapper.selectOne(wrapper);
        System.out.println("用户是否存在:" + selectOne);


        if (selectOne != null) {
            return ServerResponse.fail("用户名已存在,注册失败", "用户名已存在,注册失败");
        } else {
//            String salt = UUID.randomUUID().toString().replace("-","");
            customer.setCustomerRegistTime(LocalDateTime.now());
            customer.setCustomerStatus(0);
            int rows = customerMapper.insert(customer);

            return ServerResponse.success("注册成功", "注册成功");
        }
    }

    @Override
    public ServerResponse loginByMessage(Customer customer) {
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("customer_tel",customer.getCustomerTel());

        Customer selectOne = customerMapper.selectOne(wrapper);
        System.out.println("查询到的短信登录账户:" + selectOne);

        LoginCustomer loginCustomer=new LoginCustomer(selectOne.getCustomerId(),selectOne.getCustomerName());
        if (loginCustomer!=null){
            //2. JWT创建token
            String token= TokenUtil.sign(loginCustomer);
            System.out.println("service token:" + token);
            return ServerResponse.success("登录成功",token);
        }

        return ServerResponse.fail("登录失败",null);
    }

    @Override
    public Boolean sendMessage(Long customerTel, String code, Map<String, Object> codeMap) {
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou", "LTAI5t9nPAdswSNewNx6noCb", "4pnNVyY9VBuUK1khxbc9Qh9HFJfjT7");
        IAcsClient client = new DefaultAcsClient(profile);
// 构建请求：
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
// 自定义参数：
        System.out.println("ali:"+customerTel);
        request.putQueryParameter("PhoneNumbers", customerTel.toString());// 手机号
        request.putQueryParameter("SignName", "阿里云短信测试");// 短信签名
        request.putQueryParameter("TemplateCode", "SMS_154950909");// 短信模版CODE
// 构建短信验证码
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(codeMap));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}